package com.shopcart.service.impl;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.demo.util.SqlUtils;
import com.shopcart.domain.StoreGoodsCategory;
import com.shopcart.repository.StoreGoodsCategoryRepository;
import com.shopcart.service.StoreGoodsCategoryService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
@Transactional
public class StoreGoodsCategoryServiceImpl implements StoreGoodsCategoryService {

	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory
			.getLogger(StoreGoodsCategoryServiceImpl.class);
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	@Autowired
	private StoreGoodsCategoryRepository storeGoodsCategoryRepository;

	@Override
	public Collection<StoreGoodsCategory> findByStoreId(String storeId,
			Sort sort) {
		// TODO Auto-generated method stub
		return storeGoodsCategoryRepository.findByStoreId(storeId, sort);
	}

	@Override
	public Page<StoreGoodsCategory> findByStoreId(String storeId, Pageable p) {
		// TODO Auto-generated method stub
		return storeGoodsCategoryRepository.findByStoreId(storeId, p);
	}

	@Override
	public void delWithGoods(String storeId,String id) {
		// TODO Auto-generated method stub
		//删除该分类下商品
		String sql = "update s_store_goods set is_del = 1  where store_id = '"+storeId+"' and cate_code = '"+id+"'";
		jdbcTemplate.update(sql);
		
		storeGoodsCategoryRepository.delete(id);
		
	}

	@Override
	public int updateByPrimaryKeySelective(StoreGoodsCategory cate) {
		// TODO Auto-generated method stub
		try {
			String sql = SqlUtils.update(cate, true, " where id = '"+cate.getId()+"'");
			return jdbcTemplate.update(sql);
		}catch (Exception e) {
			// TODO Auto-generated catch block
			return 0;
		}
	}

	@Override
	public void save(StoreGoodsCategory cate) {
		// TODO Auto-generated method stub
		storeGoodsCategoryRepository.save(cate);
	}

	@Override
	public StoreGoodsCategory findById(String id) {
		// TODO Auto-generated method stub
		return storeGoodsCategoryRepository.findOne(id);
	}


	
}
